package com.itheima.mall.api.sms;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mall.mapper.sms.FlashPromotionSessionMapper;
import com.itheima.mall.mapper.sms.SmsFlashPromotionProductRelationMapper;
import com.itheima.mall.pojo.sms.SmsFlashPromotionProductRelation;
import com.itheima.mall.pojo.sms.SmsFlashPromotionSession;
import com.itheima.mall.vo.R;
import com.itheima.mall.vo.SessionVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class FlashPromotionSessionServiceApiImpl implements FlashPromotionSessionServiceApi{
   @Autowired
    FlashPromotionSessionMapper sessionMapper;
   @Autowired
    SmsFlashPromotionProductRelationMapper relationMapper;
    @Override
    public void createSession(SmsFlashPromotionSession session) {
sessionMapper.insert(session);
    }

    @Override
    public void deleteSession(Integer id) {
        sessionMapper.deleteById(Convert.toLong(id));
    }

    @Override
    public R<List<SmsFlashPromotionSession>> getList() {
        LambdaQueryWrapper<SmsFlashPromotionSession> wrapper=new LambdaQueryWrapper<>();
        List<SmsFlashPromotionSession> list = sessionMapper.selectList(wrapper);
        return R.succeed(list,"查询成功");
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        LambdaQueryWrapper<SmsFlashPromotionSession> wrapper=new LambdaQueryWrapper<>();
        SmsFlashPromotionSession session=new SmsFlashPromotionSession();
        session.setStatus(status);
        session.setId(Convert.toLong(id));
        sessionMapper.updateById(session);
    }

    @Override
    public void updateSession(SmsFlashPromotionSession session) {
        sessionMapper.updateById(session);
    }

    @Override
    public SmsFlashPromotionSession getSession(Integer id) {
        SmsFlashPromotionSession session = sessionMapper.selectById(Convert.toLong(id));
        return session;

    }

    @Override
    public List<SessionVo> selectList(Integer flashPromotionId) {
       if(flashPromotionId!=null){
           List<SessionVo> list=new ArrayList<>();
           LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper=new LambdaQueryWrapper<>();
           queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
           int count=relationMapper.count(flashPromotionId);
           //List<SmsFlashPromotionProductRelation> relations = relationMapper.selectList(queryWrapper);

           /*
           查找数据
            */
           LambdaQueryWrapper<SmsFlashPromotionSession> wrapper=new LambdaQueryWrapper<>();
           wrapper.eq(SmsFlashPromotionSession::getId,flashPromotionId);
           SmsFlashPromotionSession session = sessionMapper.selectById(Convert.toLong(flashPromotionId));
           SessionVo sessionVo=new SessionVo();
           sessionVo.setCreateTime(session.getCreateTime());
           sessionVo.setEndTime(session.getEndTime());
           sessionVo.setName(session.getName());
           sessionVo.setStartTime(session.getStartTime());
           sessionVo.setStatus(session.getStatus());
           sessionVo.setId(session.getId());
           sessionVo.setProductCount(count);
           list.add(sessionVo);
           return list;

       }else {
           LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper=new LambdaQueryWrapper<>();
           queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
           int count=relationMapper.count(flashPromotionId);
           //List<SmsFlashPromotionProductRelation> relations = relationMapper.selectList(queryWrapper);
           //取到relations和count
           LambdaQueryWrapper<SmsFlashPromotionSession> wrapper=new LambdaQueryWrapper<>();
           List<SmsFlashPromotionSession> sessions = sessionMapper.selectList(wrapper);
           List<SessionVo> list=new ArrayList<>();
           for (SmsFlashPromotionSession session : sessions) {
               SessionVo sessionVo=new SessionVo();
               sessionVo.setCreateTime(session.getCreateTime());
               sessionVo.setEndTime(session.getEndTime());
               sessionVo.setName(session.getName());
               sessionVo.setStartTime(session.getStartTime());
               sessionVo.setStatus(session.getStatus());
               sessionVo.setId(session.getId());
               sessionVo.setProductCount(count);
               list.add(sessionVo);
           }
           return list;
       }

    }
}
